package org.jeecg.modules.contract.review.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.business.entity.BussOutletQuote;
import org.jeecg.modules.business.entity.BussOutletQuotePackage;
import org.jeecg.modules.business.entity.BussOutletQuoteProduct;
import org.jeecg.modules.business.mapper.BussOutletQuoteMapper;
import org.jeecg.modules.business.mapper.BussOutletQuotePackageMapper;
import org.jeecg.modules.business.mapper.BussOutletQuoteProductMapper;
import org.jeecg.modules.contract.review.entity.BussContractRequirementReview;
import org.jeecg.modules.contract.review.entity.BussContractRequirementReviewGoods;
import org.jeecg.modules.contract.review.entity.BussContractRequirementReviewGoodspackage;
import org.jeecg.modules.contract.review.mapper.BussContractRequirementReviewGoodsMapper;
import org.jeecg.modules.contract.review.mapper.BussContractRequirementReviewGoodspackageMapper;
import org.jeecg.modules.contract.review.mapper.BussContractRequirementReviewMapper;
import org.jeecg.modules.contract.review.service.IBussContractRequirementReviewService;
import org.jeecg.modules.contract.review.vo.BussContractRequirementReviewGoodsVo;
import org.jeecg.modules.contract.review.vo.BussContractRequirementReviewGoodspackageVo;
import org.jeecg.modules.contract.review.vo.BussContractRequirementReviewVo;
import org.jeecg.modules.flowable.apithird.business.entity.FlowMyBusiness;
import org.jeecg.modules.flowable.apithird.service.FlowCallBackServiceI;
import org.jeecg.modules.flowable.apithird.service.FlowCommonService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @Description: 合同要求评审表
 * @Author: jeecg-boot
 * @Date:   2024-05-30
 * @Version: V1.0
 */
@Service("contractRequirementReviewService")
public class BussContractRequirementReviewServiceImpl extends ServiceImpl<BussContractRequirementReviewMapper, BussContractRequirementReview> implements IBussContractRequirementReviewService, FlowCallBackServiceI {

    @Autowired
    private BussContractRequirementReviewMapper bussContractRequirementReviewMapper;
    @Autowired
    private BussContractRequirementReviewGoodsMapper bussContractRequirementReviewGoodsMapper;
    @Autowired
    private BussContractRequirementReviewGoodspackageMapper bussContractRequirementReviewGoodspackageMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    FlowCommonService flowCommonService;
    @Autowired
    private BussOutletQuoteMapper bussOutletQuoteMapper;
    @Autowired
    private BussOutletQuotePackageMapper bussOutletQuotePackageMapper;
    @Autowired
    private BussOutletQuoteProductMapper bussOutletQuoteProductMapper;
    @Override
    public void afterFlowHandle(FlowMyBusiness business) {
        //流程操作后做些什么
        business.getTaskNameId();//接下来审批的节点
        business.getValues();//前端传进来的参数
        business.getActStatus();//流程状态 ActStatus.java
        //....其他

    }



    @Override
    public Object getBusinessDataById(String dataId) {
        return this.getById(dataId);
    }

    @Override
    public Map<String, Object> flowValuesOfTask(String taskNameId, Map<String, Object> values) {
        return null;
    }

    @Override
    public List<String> flowCandidateUsernamesOfTask(String taskNameId, Map<String, Object> values) {
        // 通过当前节点来判断下一个节点的候选人并写回到反参中，如果为null，流程模块会根据默认设置处理

        return null;
    }



    @Override
    public boolean save(BussContractRequirementReview testDemo) {
        /**新增数据**/
        testDemo.setId(IdUtil.fastSimpleUUID());
        //Comparison method violates its general contract!有时候出现这个错误加的，原因不明
        System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
        return super.save(testDemo);
    }
    @Override
    public boolean removeById(Serializable id) {
        /**删除数据，移除流程关联信息**/
        flowCommonService.delActBusiness(id.toString());
        return super.removeById(id);
    }

    @Override
    public String saveMain(BussContractRequirementReview bussContractRequirementReview, List<BussContractRequirementReviewGoodsVo> bussContractRequirementReviewGoodsVos) {
        //查询创建人id
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        bussContractRequirementReview.setUserId(sysUser.getId());
        bussContractRequirementReviewMapper.insert(bussContractRequirementReview);
        if (bussContractRequirementReviewGoodsVos != null && bussContractRequirementReviewGoodsVos.size() > 0) {
            for (BussContractRequirementReviewGoodsVo entity : bussContractRequirementReviewGoodsVos) {
                List<BussContractRequirementReviewGoodspackageVo> goodspackageVos = entity.getPackageVos();
                BussContractRequirementReviewGoods goods = new BussContractRequirementReviewGoods();
                BeanUtils.copyProperties(entity, goods);
                //外键设置
                goods.setReviewId(bussContractRequirementReview.getId());
                bussContractRequirementReviewGoodsMapper.insert(goods);
                if (goodspackageVos != null && goodspackageVos.size() > 0) {
                    for (BussContractRequirementReviewGoodspackageVo packageVo : goodspackageVos) {
                        BussContractRequirementReviewGoodspackage packages = new BussContractRequirementReviewGoodspackage();
                        BeanUtils.copyProperties(packageVo, packages);
                        packages.setId(String.valueOf(IdWorker.getId()));
                        //外键设置
                        packages.setReviewId(bussContractRequirementReview.getId());
                        bussContractRequirementReviewGoodspackageMapper.insert(packages);
                    }
                }
            }
        }
        return bussContractRequirementReview.getId();
    }

    @Override
    public void updateMain(BussContractRequirementReview bussContractRequirementReview, List<BussContractRequirementReviewGoodsVo> bussContractRequirementReviewGoodsVos) {
        bussContractRequirementReviewMapper.updateById(bussContractRequirementReview);
        bussContractRequirementReviewGoodsMapper.deleteByMainId(bussContractRequirementReview.getId());
        bussContractRequirementReviewGoodspackageMapper.deleteByMainId(bussContractRequirementReview.getId());
        if (bussContractRequirementReviewGoodsVos != null && bussContractRequirementReviewGoodsVos.size() > 0) {
            for (BussContractRequirementReviewGoodsVo entity : bussContractRequirementReviewGoodsVos) {
                List<BussContractRequirementReviewGoodspackageVo> goodspackageVos = entity.getPackageVos();
                BussContractRequirementReviewGoods goods = new BussContractRequirementReviewGoods();
                BeanUtils.copyProperties(entity, goods);
                //外键设置
                goods.setReviewId(bussContractRequirementReview.getId());
                bussContractRequirementReviewGoodsMapper.insert(goods);
                if (goodspackageVos != null && goodspackageVos.size() > 0) {
                    for (BussContractRequirementReviewGoodspackageVo packageVo : goodspackageVos) {
                        BussContractRequirementReviewGoodspackage packages = new BussContractRequirementReviewGoodspackage();
                        BeanUtils.copyProperties(packageVo, packages);
                        packages.setId(String.valueOf(IdWorker.getId()));
                        //外键设置
                        packages.setReviewId(bussContractRequirementReview.getId());
                        bussContractRequirementReviewGoodspackageMapper.insert(packages);
                    }
                }
            }
        }
    }

    @Override
    public IPage<BussContractRequirementReviewVo> myPage(Page<BussContractRequirementReview> page, QueryWrapper<BussContractRequirementReview> queryWrapper) {
        return this.baseMapper.myPage(page, queryWrapper);
    }

    @Override
    public void copyInfo(String oldId, String newId) {
        //复制产品
        List<BussContractRequirementReviewGoods> goodsList = bussContractRequirementReviewGoodsMapper.selectByMainId(oldId);
        for (BussContractRequirementReviewGoods goods : goodsList){
            goods.setId(null);
            goods.setReviewId(newId);
            bussContractRequirementReviewGoodsMapper.insert(goods);
        }
        //复制包材
        List<BussContractRequirementReviewGoodspackage> packagesList = bussContractRequirementReviewGoodspackageMapper.selectByMainId(oldId);
        for (BussContractRequirementReviewGoodspackage packages : packagesList){
            packages.setId(null);
            packages.setReviewId(newId);
            bussContractRequirementReviewGoodspackageMapper.insert(packages);
        }
        //复制报价
        BussOutletQuote quote = bussOutletQuoteMapper.queryByReviewId(oldId);
        if (quote != null){
            String quoteId = quote.getId();
            quote.setId(null);
            quote.setContractReviewId(newId);
            bussOutletQuoteMapper.insert(quote);
            //复制报价产品
            List<BussOutletQuoteProduct> productList = bussOutletQuoteProductMapper.selectByMainId(quoteId);
            for (BussOutletQuoteProduct product : productList){
                product.setId(null);
                product.setOutQuoteId(quote.getId());
                bussOutletQuoteProductMapper.insert(product);
            }
            //复制报价包材
            List<BussOutletQuotePackage> quotePackages = bussOutletQuotePackageMapper.selectByMainId(quoteId);
            for (BussOutletQuotePackage quotePackage : quotePackages){
                quotePackage.setId(null);
                quotePackage.setOutQuoteId(quote.getId());
                bussOutletQuotePackageMapper.insert(quotePackage);
            }
        }
    }
}
